package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static io.netty.handler.codec.DateFormatter.format;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    TbBusinessMapper tbBusinessMapper;

    /**
     * 查询跟进记录
     *
     * @param id
     * @return
     */
    @Override
    public List<TbBusinessTrackRecord> list(Long id) {
        return tbBusinessTrackRecordMapper.list(id);
    }

    /**
     * 修改商机并新增记录
     *
     * @param businessTrackVo
     * @return
     */
    @Override
    @Transactional
    public boolean add(BusinessTrackVo businessTrackVo) {
        String updateTime = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());

        System.out.println("businessTrackVo = " + businessTrackVo);

        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        tbBusiness.setUpdateTime(new Date());
        tbBusiness.setStatus("2");

        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo, tbBusinessTrackRecord);
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecord.setTrackStatus("2");
        tbBusinessTrackRecord.setCreateTime(new Date());

        Integer row1 = tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);
        Integer row2 = tbBusinessMapper.updateTbBusiness(tbBusiness);
        return row1 > 0 && row2 > 0;
    }

}
